<?php
namespace XtwsBack\Controller;
use Think\Controller;
class WithDrawController extends CommonController {

    // 店主-线下扫码支付金额提现
    public function index(){
        $member  = M('member');
        // 查询余额不为0的店铺老板信息
        $map['u_type'] = 1;
        $map['credit3'] = array('neq','0.00');
        $count = $member->where($map)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25),查出一百条记录到前台，再由前台js处理成分页
        $show = $Page->show();// 分页显示输出
        $list = $member ->order('id desc')->where($map)->limit($Page->firstRow.','.$Page->listRows)->field('nickname,realname,credit3,id')->select();
        foreach ($list as $k => $v){
            $list[$k]['storename'] = M('shop_store')->where('u_id ='.$v['id'])->getField('storename');  //店铺名称
        }
        $this -> assign('list',$list);
        $this -> assign('page',$show);
        //加载视图
        $this->display();
    }//end fun

    public function loginfo(){
        $u_id = I('u_id');
        $a_date = $_GET['date'];  //获取日期
        $member = M('member');
        $shop_pay_log = M('shop_pay_log');

        
        $date = explode("-",$a_date);
        $year = $date[0];
        $month = $date[1];
        $day = $date[2];
        $s_time = mktime(0,0,0,$month,$day,$year);      //当天开始时间戳
        $e_time= mktime(23,59,59,$month,$day,$year);    //当天结束时间戳

        //查询条件
        $where['u_id'] = $u_id;
        $where['pay_type'] = 'wxpay';
        $where['status'] = 1;
        $where['updated_at'] = array('between',array($s_time,$e_time));
        //线下扫码-微信支付
        $wxpay_list = $shop_pay_log->where($where)->select();   //收款明细
        $wxpay_fee = 0;
        foreach ($wxpay_list as $k=>$v){
            $fee = $v['amount']*0.003;
            $wxpay_list[$k]['sx_fee'] = round($fee, 2);
            $wxpay_fee += round($fee, 2);  //手续费求和
        }
        $data['wxpay_fee'] = $wxpay_fee;
        $wxpay_total = $shop_pay_log->where($where)->sum('amount');     //微信收款总金额
        $integral_fee = round($wxpay_total*0.007, 2);          //积分费四舍五入
        $service_fee = round($wxpay_total*0.006, 2);          //积分费四舍五入
        $data['integral_fee'] = $integral_fee;
        $data['service_fee'] = $service_fee;

        //线下扫码-支付宝支付
        $map = $where;
        $map['pay_type'] = 'alipay';
        $alipay_list = $shop_pay_log->where($map)->select();    //收款明细表
        $alipay_fee = 0;
        foreach ($alipay_list as $k=>$v){
            $fee = $v['amount']*0.003;
            $fee = round($fee, 2);
            $alipay_list[$k]['sx_fee'] = round($fee, 2);
            $alipay_fee += $fee;  //手续费求和
        }

        $data['alipay_fee'] = $alipay_fee;
        $alipay_total = $shop_pay_log->where($map)->sum('amount');      //支付宝收款总金额

        //微信可提现金额
        $data['wx_total_fee'] = round($wxpay_total-$wxpay_fee-$integral_fee-$service_fee,2);
        //支付宝可提现金额
        $data['ali_total_fee'] = round($alipay_total-$alipay_fee,2);

        //查询用户是否已经提现
        $data['is_tx'] = M('member_credit_log')->where("`u_id` = {$u_id} AND `shop_credit` = 10 AND `clearingdate` LIKE '{$a_date}'" )->getField('id');
//        dump($data);
        $m_info  = $member->where("id={$u_id}")->field('nickname,realname,credit3,id')->find();
        $m_info['storename'] = M('shop_store')->where('u_id='.$u_id)->getField('storename');

        $this -> assign('date',$a_date);
        $this -> assign('wxpay_list',$wxpay_list);
        $this -> assign('alipay_list',$alipay_list);
        $this -> assign('wxpay_total',$wxpay_total);
        $this -> assign('alipay_total',$alipay_total);
        $this -> assign('m_info',$m_info);
        $this -> assign($data);
        //加载视图
        $this->display();
    }// end fun

    // 提现-余额变动
    public function changeCredit3(){
        if(IS_POST){
            $post =  I('post.');
            $u_id = $post['u_id'];
            $date = $post['date'];
            // 查询用户是否已提现
            $is_tx = M('member_credit_log')->where("`u_id` = {$u_id} AND `shop_credit` = 10 AND `clearingdate` LIKE '{$date}'" )->getField('id');
            if($is_tx>0){
                $this->success('caa');
            }else{
                // 用户余额减少
                $res = M('member')->where('id = '.$post['u_id'])->setDec('credit3',$post['credit3']);
                if($res){
                    // 增加余额变动记录
                    $result = changeCredit($post['u_id'],'提现（线下扫码支付）','credit3','-',$post['credit3'],10,$date);
                    $this->success('操作成功');
                }
            }

        }else{
            $this->error('非法进入');
        }//end if
    }//end fun


}// end class